package com.lzw.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lzw.bean.*;
import com.lzw.common.bean.Result;
import com.lzw.common.controller.BaseController;
import com.lzw.common.service.impl.CommonService;
import com.lzw.param.node.Page;
import com.lzw.service.MenuNodeService;
import com.lzw.service.MenuService;
import com.lzw.service.NodeService;
import com.lzw.service.RoleMenuService;
import com.lzw.service.impl.RoleMenuServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 林志伟 on 2017/5/11.
 */
@Controller()
@RequestMapping("/menu")
public class MenuController extends BaseController {
    @Autowired
    private  MenuService menuService;

    @Autowired
    private MenuNodeService menuNodeService;

    @GetMapping("/index")
    public String  index(Model model, Page page){
        return this.getTemplateName();
    }


    @PostMapping("/menulist")
    @ResponseBody
    public Result menulist(){
        return  CommonService.success("加载成功",menuService.select(100000));
    }

    @GetMapping("/add")
    public String add(@RequestParam int pId, Model model){

       Menu menu=menuService.find(pId);

        model.addAttribute("pmenu",menu);
        return this.getTemplateName();
    }

    @PostMapping("/add")
    @ResponseBody
    public Result addPost(@Valid Menu menu, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return CommonService.error(bindingResult.getFieldError().getDefaultMessage());
        }
        Result result=menuService.insert(menu);
        return result;
    }
//
    @GetMapping("/update")
    public String update(@RequestParam int id, Model model){
        Menu menu =menuService.find(id);
        Menu pmenu =menuService.find(menu.getpId());
        model.addAttribute("menu",menu);
        model.addAttribute("pmenu",pmenu);
        return this.getTemplateName();
    }
//
    @ResponseBody
    @PostMapping("/update")
    public Result updatePost(Menu menu, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return CommonService.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return menuService.update(menu);
    }
//
    @PostMapping("/delete")
    @ResponseBody
    public  Result delete(@RequestParam("id") int id){
        return  menuService.delete(id);
    }


    private @Autowired NodeService nodeService;
    @GetMapping("/setNode")
    public String  setNode(@RequestParam("menu_id")  int menu_id,Model model){
        List<Node> nodes=nodeService.selectByMenu(menu_id);
        model.addAttribute("nodes",nodes);
        Menu menu=menuService.find(menu_id);
        model.addAttribute("menu",menu);
        return  this.getTemplateName();
    }


    /**
     * 修改授权信息
     * @param menu_id
     * @param node_ids
     * @return
     */
    @PostMapping("/setNode")
    @ResponseBody
    public Result setNodePost(@RequestParam("menu_id")  int menu_id,@RequestParam("node_ids[]") int[] node_ids){
        return  menuNodeService.setNode(menu_id,node_ids);
    }

}
